package com.freemyleft.left.zapp.selectimage.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.freemyleft.left.R;
import com.freemyleft.left.zapp.selectimage.PicassoUtils;
import com.freemyleft.left.zapp.selectimage.bean.Image;


import java.util.ArrayList;
import java.util.List;


/**
 * 大图展示
 */
public class ImageShowActivity extends AppCompatActivity {


    ViewPager mViewpage;
    TextView tvImageNum;
    private int mPosition = 0;
    private List<Image> mListImage = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_show);
        initView();
    }

    public void initView() {
        mViewpage = (ViewPager) findViewById(R.id.m_viewpage);
        tvImageNum = (TextView) findViewById(R.id.tv_image_num);

        initData();
    }


    public void initData() {
        mListImage = (List<Image>) getIntent().getSerializableExtra("images");
        mPosition = getIntent().getIntExtra("position",0);
        showImageInfo(mPosition);
        MyPagerAdapter adapter = new MyPagerAdapter(ImageShowActivity.this);
        mViewpage.setAdapter(adapter);
        mViewpage.setCurrentItem(mListImage.size() * 1000 + mPosition);
        mViewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                showImageInfo(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void showImageInfo(int position) {
        tvImageNum.setText(((position % mListImage.size())+1)+"/"+mListImage.size());
    }



    private class MyPagerAdapter extends PagerAdapter {


        public MyPagerAdapter(Context mContext) {

        }

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            PhotoView imageView = new PhotoView(ImageShowActivity.this);
            try {
                //imageView.setMaxScale(10);
                imageView.enable();
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                // 设置动画的插入器
                imageView.setInterpolator(new AccelerateDecelerateInterpolator());
                Log.e("haha", "position % mListImage.size()::"+ position % mListImage.size());
                PicassoUtils.picasso(ImageShowActivity.this, mListImage.get(position % mListImage.size()).path, mListImage.get(position % mListImage.size()).width , mListImage.get(position % mListImage.size()).height, imageView, true);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                container.addView(imageView);
            } catch (Exception e) {

            }
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
